package cinescope2014.boundaries;

import java.sql.*;

/**
 *
 * @author pascal
 */
public class JIFPaysInsert extends javax.swing.JInternalFrame {

    /**
     * Creates new form JIFPaysSelect
     */
    public JIFPaysInsert() {
        initComponents();

        setTitle("Nouveau Pays");
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabelMessage = new javax.swing.JLabel();
        jButtonAjouter = new javax.swing.JButton();
        jTextFieldNomPays = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldMasculin = new javax.swing.JTextField();
        jTextFieldFeminin = new javax.swing.JTextField();
        jTextFieldNeutre = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        jLabel1.setText("Nom du pays");

        jLabelMessage.setText("Message");

        jButtonAjouter.setText("Ajouter");
        jButtonAjouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterActionPerformed(evt);
            }
        });

        jTextFieldNomPays.setText("Roumanie");

        jLabel2.setText("Masculin");

        jLabel3.setText("Féminin");

        jLabel4.setText("Neutre");

        jTextFieldMasculin.setText("roumain");

        jTextFieldFeminin.setText("roumain");

        jTextFieldNeutre.setText("roumano");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonAjouter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(51, 51, 51)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextFieldNomPays, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                .addComponent(jTextFieldMasculin)
                                .addComponent(jTextFieldFeminin)
                                .addComponent(jTextFieldNeutre))))
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldNomPays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldMasculin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldFeminin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldNeutre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButtonAjouter)
                .addGap(18, 18, 18)
                .addComponent(jLabelMessage)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterActionPerformed
        // TODO add your handling code here:

        if (jTextFieldNomPays.getText().equals("") || jTextFieldMasculin.getText().equals("") || jTextFieldFeminin.getText().equals("") || jTextFieldNeutre.getText().equals("")) {
            jLabelMessage.setText("Toutes les saisies sont obligatoires !!!");
        } else {
            try {
                // --- Connexion
                Class.forName("org.gjt.mm.mysql.Driver");
                String lsDSN = "jdbc:mysql://127.0.0.1:3306/cinescope2014";
                Connection cn = DriverManager.getConnection(lsDSN, "root", "");

                // INSERTION
                String lsSQL = "INSERT INTO pays(nom_pays, masculin, feminin, neutre) VALUES(?,?,?,?)";

                PreparedStatement lpst = cn.prepareStatement(lsSQL);

                lpst.setString(1, jTextFieldNomPays.getText());
                lpst.setString(2, jTextFieldMasculin.getText());
                lpst.setString(3, jTextFieldFeminin.getText());
                lpst.setString(4, jTextFieldNeutre.getText());

                lpst.executeUpdate();

                lpst.close();
                cn.close();

                jLabelMessage.setText("Okay !!!");

            } catch (ClassNotFoundException | SQLException ex) {
                jLabelMessage.setText(ex.getMessage());
            }
        }

    }//GEN-LAST:event_jButtonAjouterActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAjouter;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JTextField jTextFieldFeminin;
    private javax.swing.JTextField jTextFieldMasculin;
    private javax.swing.JTextField jTextFieldNeutre;
    private javax.swing.JTextField jTextFieldNomPays;
    // End of variables declaration//GEN-END:variables
}
